// api/axios.js
import axios from 'axios';

const instance = axios.create({
    // baseURL: 'http://127.0.0.1:8080',
    timeout: 10000, // 设置请求超时时间为10秒
    headers: {
        'Content-Type': 'application/json',
    },
});

// 请求拦截器
instance.interceptors.request.use(
    config => {
        // 在发送请求之前做些什么
        // 例如添加认证头
        const token = localStorage.getItem('token');
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }

        //根据服务名称动态修改请求地址
        switch (config.serviceName) {
            case 'departmentService':
                config.url = `http://127.0.0.1:8081${config.url}`;
                break;
            case 'empService':
                config.url = `http://127.0.0.1:8082${config.url}`;
                break;
            case 'employeeService':
                config.url = `http://127.0.0.1:8083${config.url}`;
                break;
            case 'homeService':
                config.url = `http://127.0.0.1:8084${config.url}`;
                break;
            case 'logService':
                config.url = `http://127.0.0.1:8085${config.url}`;
                break;
            case 'loginService':
                config.url = `http://127.0.0.1:8086${config.url}`;
                break;
            case 'managerService':
                config.url = `http://127.0.0.1:8087${config.url}`;
                break;
            case 'noticeService':
                config.url = `http://127.0.0.1:8088${config.url}`;
                break;
            case 'propertitesService':
                config.url = `http://127.0.0.1:8089${config.url}`;
                break;
            case 'roleService':
                config.url = `http://127.0.0.1:8090${config.url}`;
                break;
            case 'studyService':
                config.url = `http://127.0.0.1:8091${config.url}`;
                break;
            case 'workService':
                config.url = `http://127.0.0.1:8092${config.url}`;
                break;
            case 'salaryService':
                config.url = `http://127.0.0.1:8095${config.url}`;
                break;
            case 'uploadService':
                config.url = `http://127.0.0.1:8096${config.url}`;
                break;
            default:
                break;
        }

        return config;
    },
    error => {
        // 处理请求错误
        return Promise.reject(error);
    }
);

// 响应拦截器
instance.interceptors.response.use(
    response => {
        // 对响应数据做点什么
        return response;
    },
    error => {
        // 处理响应错误
        if (error.response) {
            // The request was made and the server responded with a status code
            // that falls out of the range of 2xx
            console.error('Response error:', error.response.data);
        } else if (error.request) {
            // The request was made but no response was received
            console.error('No response received:', error.request);
        } else {
            // Something happened in setting up the request that triggered an Error
            console.error('Error setting up request:', error.message);
        }
        console.error('Error config:', error.config);

        // 返回一个 Promise.reject，以便在调用者处处理错误
        return Promise.reject(error);
    }
);

export default instance;